--訏谟已定命
local xumoyidingming = fk.CreateSkill {
    name = "mcdh_xumoyidingming",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

xumoyidingming:addEffect(fk.AfterCardsMove,{
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(self) and player:getMark("mcdh_charge") >=3 then
            for _, move in ipairs(data) do
                if (move.from == player.id or move.to == player.id) and move.to ~= move.from and move.toArea == Card.PlayerHand  then
                    for _, info in ipairs(move.moveInfo) do
                        if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and Fk:getCardById(info.cardId).color == Card.Red and player.room:getCardArea(info.cardId) == Card.PlayerHand then
                            return true
                        end
                    end
                end
            end
        end
    end,
    on_trigger = function(self, event, target, player, data)
        local targets = {}
        data.extra_data = data.extra_data or {}
        data.extra_data.mcdh_XMYDM = data.extra_data.mcdh_XMYDM or {}
        local room = player.room
        for _, move in ipairs(data) do
            if (move.from == player.id or move.to == player.id) and move.to ~= move.from and move.toArea == Card.PlayerHand   then
                for _, info in ipairs(move.moveInfo) do
                    if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and Fk:getCardById(info.cardId).color == Card.Red and room:getCardArea(info.cardId) == Card.PlayerHand then
                        table.insertIfNeed(targets, move.to)
                        data.extra_data.mcdh_XMYDM[move.to] = data.extra_data.mcdh_XMYDM[move.to] or {}
                        table.insertIfNeed(data.extra_data.mcdh_XMYDM[move.to],info.cardId)
                    end
                end
            end
        end
        room:sortPlayersByAction(targets)
        for _, p in ipairs(targets) do
            local to = room:getPlayerById(p)
            if to.dead or not player:hasSkill(self) then
                break
            end
            self:doCost(event, to, player, data)
        end
        return false
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        return room:askForSkillInvoke(player, self.name, nil, "#xumoyidingming-invoke:"..target.id)
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        lib.mcdh_charge(player,-3)
        if data.extra_data and data.extra_data.mcdh_XMYDM and data.extra_data.mcdh_XMYDM[target.id] then
            room:throwCard(data.extra_data.mcdh_XMYDM[target.id],self.name,target,player)
        end
        room:recover({
            who = target,
            num = 1,
            recoverBy =player ,
            skillName = self.name
        })
    end
})

Fk:loadTranslationTable {
    ["mcdh_xumoyidingming"] = "訏谟",
    [":mcdh_xumoyidingming"]= "『訏谟已定命』<br>蓄力技(3)，当你获得其他角色的红色牌后，或其他角色获得你的红色牌后，你可以弃置此牌，然后令获得红色牌的角色回复1点体力。",
    ["#xumoyidingming-invoke"] = "訏谟已定命：弃置 %src 获得的红色牌并令其回复1点体力"
}

return xumoyidingming